from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain.text_splitter import SpacyTextSplitter
from langchain import VectorDBQA
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import TextLoader
import openai, os

os.environ["OPENAI_API_KEY"] = "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"
openai.api_key = os.environ.get("OPENAI_API_KEY")
llm = ChatOpenAI(model_name='gpt-3.5-turbo',max_tokens=2048, temperature=0.5)

loader = TextLoader('ecommerce_faq.txt')
documents = loader.load()
text_splitter = SpacyTextSplitter(chunk_size=256, pipeline="zh_core_web_sm")
texts = text_splitter.split_documents(documents)

embeddings = OpenAIEmbeddings()
docsearch = FAISS.from_documents(texts, embeddings)


faq_chain = VectorDBQA.from_chain_type(llm=llm, vectorstore=docsearch, verbose=True)